<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="图片url" prop="img">
      <el-input v-model="dataForm.img" placeholder="图片url"></el-input>
    </el-form-item>
    <el-form-item label="地区id" prop="aId">
      <el-input v-model="dataForm.aId" placeholder="地区id"></el-input>
    </el-form-item>
    <el-form-item label="1 删除;2 没有删除" prop="isDelete">
      <el-input v-model="dataForm.isDelete" placeholder="1 删除;2 没有删除"></el-input>
    </el-form-item>
    <el-form-item label="1 展示;2 未展示" prop="isShow">
      <el-input v-model="dataForm.isShow" placeholder="1 展示;2 未展示"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="sort">
      <el-input v-model="dataForm.sort" placeholder="排序"></el-input>
    </el-form-item>
    <el-form-item label="跳转路径" prop="path">
      <el-input v-model="dataForm.path" placeholder="跳转路径"></el-input>
    </el-form-item>
    <el-form-item label="公司名字" prop="company">
      <el-input v-model="dataForm.company" placeholder="公司名字"></el-input>
    </el-form-item>
    <el-form-item label="截止日期" prop="endTime">
      <el-input v-model="dataForm.endTime" placeholder="截止日期"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          img: '',
          aId: '',
          isDelete: '',
          isShow: '',
          createTime: '',
          sort: '',
          path: '',
          company: '',
          endTime: '',
          remark: ''
        },
        dataRule: {
          img: [
            { required: true, message: '图片url不能为空', trigger: 'blur' }
          ],
          aId: [
            { required: true, message: '地区id不能为空', trigger: 'blur' }
          ],
          isDelete: [
            { required: true, message: '1 删除;2 没有删除不能为空', trigger: 'blur' }
          ],
          isShow: [
            { required: true, message: '1 展示;2 未展示不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          sort: [
            { required: true, message: '排序不能为空', trigger: 'blur' }
          ],
          path: [
            { required: true, message: '跳转路径不能为空', trigger: 'blur' }
          ],
          company: [
            { required: true, message: '公司名字不能为空', trigger: 'blur' }
          ],
          endTime: [
            { required: true, message: '截止日期不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/bbl/calousels/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.img = data.calousels.img
                this.dataForm.aId = data.calousels.aId
                this.dataForm.isDelete = data.calousels.isDelete
                this.dataForm.isShow = data.calousels.isShow
                this.dataForm.createTime = data.calousels.createTime
                this.dataForm.sort = data.calousels.sort
                this.dataForm.path = data.calousels.path
                this.dataForm.company = data.calousels.company
                this.dataForm.endTime = data.calousels.endTime
                this.dataForm.remark = data.calousels.remark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/bbl/calousels/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'img': this.dataForm.img,
                'aId': this.dataForm.aId,
                'isDelete': this.dataForm.isDelete,
                'isShow': this.dataForm.isShow,
                'createTime': this.dataForm.createTime,
                'sort': this.dataForm.sort,
                'path': this.dataForm.path,
                'company': this.dataForm.company,
                'endTime': this.dataForm.endTime,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
